package com.wudl.kafka;
import com.wudl.avro.AvroSerializer;
import org.apache.avro.specific.SpecificRecordBase;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;
import java.util.Properties;
/**
 * @author ：wudl
 * @date ：Created in 2021-12-17 0:32
 * @description：
 * @modified By：
 * @version: 1.0
 */

public class KafkaPro<T extends SpecificRecordBase> {
    KafkaProducer<String, T> producer = new KafkaProducer<>(getProperties());

    //1.获取kafka生产对象
    public Properties getProperties(){

        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers","192.168.1.100:9092");
        properties.setProperty("acks","1");
        properties.setProperty("retries","0");
        properties.setProperty("batch.size","16384");
        properties.setProperty("linger.ms","1");
        properties.setProperty("buffer.memory","33554432");
        properties.setProperty("key.serializer", StringSerializer.class.getName());
        properties.setProperty("value.serializer", AvroSerializer.class.getName());

        return properties;
    }

    //2.发送数据
    public void sendData(String topic ,T data){
        producer.send(new ProducerRecord(topic,data));
    }
}